package com.giaothong.customview.custommapview;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class TapControlledMapView extends MapView implements OnGestureListener {
	/*
	 * This is the time in milliseconds the user has to hold
	 * the screen down for, for the long press
	 * listener to be called. 600 milliseconds is a good
	 * benchmark
	 */
	public static final int LONG_PRESS_THRESHOLD = 600;
	private GeoPoint lastMapCentre;//This is the last map centre
	private Timer longPressTimer = new Timer();//This is the timer for the long press hold
	private TapControlledMapView.onLongPressListener longPressListener;//This is the long press listener

    private GestureDetector gd;    
    private OnSingleTapListener singleTapListener;
    
	/**
	 * This is the public interface for a 
	 * long press listener
	 * @author Hussain
	 *
	 */
	public interface onLongPressListener{
		public void onLongPress(MapView mapView, GeoPoint longPressLocation);
	}
    
    
	/*
	 * Here are the constructors for the class
	 * TapControlledMapView these are required because
	 * you are extending the map view.
	 */

	public TapControlledMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupGestures();
    }

    public TapControlledMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setupGestures();
    }

    public TapControlledMapView(Context context, String apiKey) {
        super(context, apiKey);
        setupGestures();
    }
    
    private void setupGestures() {
    	gd = new GestureDetector(this);  
        
        //set the on Double tap listener  
        gd.setOnDoubleTapListener(new OnDoubleTapListener() {

			public boolean onSingleTapConfirmed(MotionEvent e) {
				if (singleTapListener != null) {
					return singleTapListener.onSingleTap(e);
				} else {
					return false;
				}
			}

			public boolean onDoubleTap(MotionEvent e) {
				TapControlledMapView.this.getController().zoomInFixing((int) e.getX(), (int) e.getY());
				return false;
			}

			public boolean onDoubleTapEvent(MotionEvent e) {
				return false;
			}
        	
        });
    }
    

	
	public void setOnSingleTapListener(OnSingleTapListener singleTapListener) {
		this.singleTapListener = singleTapListener;
	}

	public boolean onDown(MotionEvent e) {
		return false;
	}

	public void onShowPress(MotionEvent e) {}

	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	public void onLongPress(MotionEvent e) {}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return false;
	}
	
	
	
	
	
	
	
	
	
	
	/**
	 * This is setting the long press listener
	 * for the this class
	 * @param longPressListener this is the long press
	 * listener that is being passed
	 */
	public void setOnLongPressListener(TapControlledMapView.onLongPressListener longPressListener){
		this.longPressListener = longPressListener;
	}
	
	/* (non-Javadoc)
	 * @see com.google.android.maps.MapView#onTouchEvent(android.view.MotionEvent)
	 */
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (this.gd.onTouchEvent(ev)) {
			return true;
		} else {
			handleLongPress(ev);
			return super.onTouchEvent(ev);
		}
	}


	/**
	 * This is the method where the long press
	 * action will be handled
	 * @param motionEvent This is the motion event
	 * this is the user pressing down on the screen.
	 */
	private void handleLongPress(final MotionEvent motionEvent){
		/*
		 * This is the user pressing down on the 
		 * screen
		 */
		if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
		{
			longPressTimer = new Timer();
			longPressTimer.schedule(new TimerTask() {
				
				@Override
				public void run() {
					GeoPoint longPressLocation = getProjection().fromPixels((int)motionEvent.getX(), (int)motionEvent.getY());
					longPressListener.onLongPress(TapControlledMapView.this, longPressLocation);
				}
			}, LONG_PRESS_THRESHOLD);
			lastMapCentre = getMapCenter();
		}
		/*
		 * This is the user moving their finger
		 * on the screen
		 */
		if(motionEvent.getAction() == MotionEvent.ACTION_MOVE)
		{
			if(!getMapCenter().equals(lastMapCentre))
			{
				longPressTimer.cancel();
			}
			lastMapCentre = getMapCenter();
		}
		/*
		 * This is the user lifting their
		 * finger off the screen
		 */
		if(motionEvent.getAction() == MotionEvent.ACTION_UP)
		{
			longPressTimer.cancel();
		}
		/*
		 * This is the case when the user has more than one
		 * finger on the screen
		 */
		if(motionEvent.getPointerCount() > 1)
		{
			longPressTimer.cancel();
		}
	}
	
	
	
	
    
}


